/*
 * Created by JFormDesigner on Wed Mar 02 14:46:33 CST 2022
 */

package zy.blue7.im.gui;

import zy.blue7.im.ClientContainer;
import zy.blue7.im.common.message.GroupMessage;
import zy.blue7.im.common.message.SingleMessage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author unknown
 */
public class ChatIndexFrame extends JFrame {
    private static JFrame chatIndexFrame = null;
    public static JTextArea textArea = null;

    public ChatIndexFrame() {
        initComponents();
        ListModel<String> model = new DefaultComboBoxModel<>(new String[]{"张三", "李四", "王五", "赵六", "钱七"});
        list1.setFont(new Font("宋体", Font.BOLD, 30));
        list1.setModel(model);
        textArea = textArea1;
        textArea1.setFont(new Font("宋体", Font.BOLD, 20));

        ListModel<String> model1 = new DefaultComboBoxModel<>(new String[]{"group1", "group2", "group3", "group4", "group5"});
        list2.setFont(new Font("宋体", Font.BOLD, 30));
        list2.setModel(model1);
        textArea2.setFont(new Font("宋体", Font.BOLD, 20));
    }


    private void findPersonHandler(MouseEvent e) {
        CardLayout cardLayout = (CardLayout) panel2.getLayout();
        cardLayout.show(panel2, "card1");
        ListModel<String> model = new DefaultComboBoxModel<>(new String[]{"张三", "李四", "王五", "赵六", "钱七"});
        list1.setFont(new Font("宋体", Font.BOLD, 20));
        list1.setModel(model);
        textArea = textArea1;

    }

    private void findGroupHandler(MouseEvent e) {
        CardLayout cardLayout = (CardLayout) panel2.getLayout();
        cardLayout.show(panel2, "card2");
        ListModel<String> model1 = new DefaultComboBoxModel<>(new String[]{"group1", "group2", "group3", "group4", "group5"});
        list2.setFont(new Font("宋体", Font.BOLD, 30));
        list2.setModel(model1);
        textArea = textArea2;
    }

    private void sendMessage(ActionEvent e) {
        String to = (String) list1.getSelectedValue();

        String content = textField1.getText();

        SingleMessage singleMessage = new SingleMessage();
        singleMessage.setFrom(ClientContainer.sessionMap.get(LoginFrame.clientChannel));
        singleMessage.setTo(to);
        singleMessage.setContent(content);

        LoginFrame.clientChannel.writeAndFlush(singleMessage);

    }

    private void findGroupHandler(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) panel2.getLayout();
        cardLayout.show(panel2, "card3");
    }

    private void sendGroupMessage(ActionEvent e) {
        String to = (String) list2.getSelectedValue();

        String content = textField2.getText();

        GroupMessage groupMessage = new GroupMessage();
        groupMessage.setFrom(ClientContainer.sessionMap.get(LoginFrame.clientChannel));
        groupMessage.setTo(to);
        groupMessage.setContent(content);

        LoginFrame.clientChannel.writeAndFlush(groupMessage);
    }

    private void textSendGroupMessage(ActionEvent e) {
        sendGroupMessage(e);
    }

    private void textSendMessage(ActionEvent e) {
        sendMessage(e);
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        panel1 = new JPanel();
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        menu2 = new JMenu();
        menu3 = new JMenu();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel6 = new JPanel();
        scrollPane1 = new JScrollPane();
        list1 = new JList();
        scrollPane2 = new JScrollPane();
        textArea1 = new JTextArea();
        panel7 = new JPanel();
        button1 = new JButton();
        textField1 = new JTextField();
        panel4 = new JPanel();
        panel8 = new JPanel();
        scrollPane3 = new JScrollPane();
        list2 = new JList();
        scrollPane4 = new JScrollPane();
        textArea2 = new JTextArea();
        panel9 = new JPanel();
        button2 = new JButton();
        textField2 = new JTextField();
        panel5 = new JPanel();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new CardLayout());

        //======== panel1 ========
        {
            panel1.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing. border .EmptyBorder (
            0, 0 ,0 , 0) ,  "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e" , javax. swing .border . TitledBorder. CENTER ,javax . swing. border .TitledBorder
            . BOTTOM, new java. awt .Font ( "D\u0069al\u006fg", java .awt . Font. BOLD ,12 ) ,java . awt. Color .
            red ) ,panel1. getBorder () ) ); panel1. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java .
            beans. PropertyChangeEvent e) { if( "\u0062or\u0064er" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } );
            panel1.setLayout(null);

            //======== menuBar1 ========
            {

                //======== menu1 ========
                {
                    menu1.setText("\u8054\u7cfb\u4eba");
                    menu1.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            findPersonHandler(e);
                        }
                    });
                }
                menuBar1.add(menu1);

                //======== menu2 ========
                {
                    menu2.setText("\u5206\u7ec4");
                    menu2.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            findGroupHandler(e);
                        }
                    });
                }
                menuBar1.add(menu2);

                //======== menu3 ========
                {
                    menu3.setText("\u5176\u4ed6");
                }
                menuBar1.add(menu3);
            }
            panel1.add(menuBar1);
            menuBar1.setBounds(5, 5, 1105, 35);

            //======== panel2 ========
            {
                panel2.setLayout(new CardLayout());

                //======== panel3 ========
                {
                    panel3.setLayout(null);

                    //======== panel6 ========
                    {
                        panel6.setLayout(null);

                        //======== scrollPane1 ========
                        {
                            scrollPane1.setViewportView(list1);
                        }
                        panel6.add(scrollPane1);
                        scrollPane1.setBounds(15, 0, 165, 640);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < panel6.getComponentCount(); i++) {
                                Rectangle bounds = panel6.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = panel6.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            panel6.setMinimumSize(preferredSize);
                            panel6.setPreferredSize(preferredSize);
                        }
                    }
                    panel3.add(panel6);
                    panel6.setBounds(0, 0, 200, 640);

                    //======== scrollPane2 ========
                    {
                        scrollPane2.setViewportView(textArea1);
                    }
                    panel3.add(scrollPane2);
                    scrollPane2.setBounds(200, 0, 900, 545);

                    //======== panel7 ========
                    {
                        panel7.setLayout(null);

                        //---- button1 ----
                        button1.setText("\u53d1\u9001");
                        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 6f));
                        button1.addActionListener(e -> sendMessage(e));
                        panel7.add(button1);
                        button1.setBounds(795, 10, 103, 55);

                        //---- textField1 ----
                        textField1.addActionListener(e -> textSendMessage(e));
                        panel7.add(textField1);
                        textField1.setBounds(0, 15, 785, 45);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < panel7.getComponentCount(); i++) {
                                Rectangle bounds = panel7.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = panel7.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            panel7.setMinimumSize(preferredSize);
                            panel7.setPreferredSize(preferredSize);
                        }
                    }
                    panel3.add(panel7);
                    panel7.setBounds(200, 555, 900, 80);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel3.getComponentCount(); i++) {
                            Rectangle bounds = panel3.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel3.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel3.setMinimumSize(preferredSize);
                        panel3.setPreferredSize(preferredSize);
                    }
                }
                panel2.add(panel3, "card1");

                //======== panel4 ========
                {
                    panel4.setLayout(null);

                    //======== panel8 ========
                    {
                        panel8.setLayout(null);

                        //======== scrollPane3 ========
                        {
                            scrollPane3.setViewportView(list2);
                        }
                        panel8.add(scrollPane3);
                        scrollPane3.setBounds(15, 0, 165, 640);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < panel8.getComponentCount(); i++) {
                                Rectangle bounds = panel8.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = panel8.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            panel8.setMinimumSize(preferredSize);
                            panel8.setPreferredSize(preferredSize);
                        }
                    }
                    panel4.add(panel8);
                    panel8.setBounds(0, 0, 200, 640);

                    //======== scrollPane4 ========
                    {
                        scrollPane4.setViewportView(textArea2);
                    }
                    panel4.add(scrollPane4);
                    scrollPane4.setBounds(200, 0, 900, 545);

                    //======== panel9 ========
                    {
                        panel9.setLayout(null);

                        //---- button2 ----
                        button2.setText("\u53d1\u9001");
                        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 6f));
                        button2.addActionListener(e -> sendGroupMessage(e));
                        panel9.add(button2);
                        button2.setBounds(795, 10, 103, 55);

                        //---- textField2 ----
                        textField2.addActionListener(e -> textSendGroupMessage(e));
                        panel9.add(textField2);
                        textField2.setBounds(0, 15, 785, 45);

                        {
                            // compute preferred size
                            Dimension preferredSize = new Dimension();
                            for(int i = 0; i < panel9.getComponentCount(); i++) {
                                Rectangle bounds = panel9.getComponent(i).getBounds();
                                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                            }
                            Insets insets = panel9.getInsets();
                            preferredSize.width += insets.right;
                            preferredSize.height += insets.bottom;
                            panel9.setMinimumSize(preferredSize);
                            panel9.setPreferredSize(preferredSize);
                        }
                    }
                    panel4.add(panel9);
                    panel9.setBounds(200, 555, 900, 80);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel4.getComponentCount(); i++) {
                            Rectangle bounds = panel4.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel4.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel4.setMinimumSize(preferredSize);
                        panel4.setPreferredSize(preferredSize);
                    }
                }
                panel2.add(panel4, "card2");

                //======== panel5 ========
                {
                    panel5.setBackground(new Color(51, 51, 255));
                    panel5.setLayout(null);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < panel5.getComponentCount(); i++) {
                            Rectangle bounds = panel5.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = panel5.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        panel5.setMinimumSize(preferredSize);
                        panel5.setPreferredSize(preferredSize);
                    }
                }
                panel2.add(panel5, "card3");
            }
            panel1.add(panel2);
            panel2.setBounds(5, 45, 1100, 640);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1, "card1");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JPanel panel1;
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JMenu menu2;
    private JMenu menu3;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel6;
    private JScrollPane scrollPane1;
    private JList list1;
    private JScrollPane scrollPane2;
    private JTextArea textArea1;
    private JPanel panel7;
    private JButton button1;
    private JTextField textField1;
    private JPanel panel4;
    private JPanel panel8;
    private JScrollPane scrollPane3;
    private JList list2;
    private JScrollPane scrollPane4;
    private JTextArea textArea2;
    private JPanel panel9;
    private JButton button2;
    private JTextField textField2;
    private JPanel panel5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    public static void main(String[] args) {
        ChatIndexFrame chatIndexFrame = new ChatIndexFrame();
        chatIndexFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chatIndexFrame.setVisible(true);
    }

    public static void start() {
        chatIndexFrame = new ChatIndexFrame();
        chatIndexFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chatIndexFrame.setVisible(true);
    }

    public static void close() {
        if (null != chatIndexFrame)
            chatIndexFrame.dispose();
    }
}
